package com.hwhhhh.mygrad.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hwhhhh.mygrad.BaseActivity;
import com.hwhhhh.mygrad.ClsInfoActivity;
import com.hwhhhh.mygrad.CommentActivity;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.dto.CommentDTO;
import com.hwhhhh.mygrad.fragment.BottomHintFragment;
import com.hwhhhh.mygrad.interfaces.CommentListener;
import com.hwhhhh.mygrad.utils.StringUtils;
import com.hwhhhh.mygrad.view.IconfontView;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentHolder> {
    private List<CommentDTO> commentEntityList;
    private Context mContext;

    public CommentAdapter(Context context, List<CommentDTO> commentEntityList) {
        this.mContext = context;
        this.commentEntityList = commentEntityList;
    }

    @NonNull
    @Override
    public CommentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new CommentHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentHolder holder, int position) {
        CommentDTO commentEntity = commentEntityList.get(position);
        Glide.with(holder.rootView).load(commentEntity.getuProfilePhoto()).into(holder.headPortrait);
        holder.name.setText(commentEntity.getUname());
        holder.content.setText(commentEntity.getContent());
        if (commentEntity.getChildCommentNum() != 0) {
            holder.comment.setText(generateCommentText(commentEntity.getChildCommentNum()));
            holder.comment.setOnClickListener(view -> {
                Intent in = new Intent(mContext, CommentActivity.class);
                in.putExtra("commentParent", commentEntity);
                mContext.startActivity(in);
            });
        } else {
            holder.comment.setVisibility(View.GONE);
            holder.icComment.setVisibility(View.GONE);
        }
        holder.like.setText(generateLikeText(commentEntity.getLikes()));
        // 点赞的点击监听
        View.OnClickListener likeOnClick = v -> {
            holder.icLike.setText(mContext.getResources().getString(R.string.ic_like_selected));
            holder.like.setText(generateLikeText(commentEntity.getLikes() + 1));
        };
        holder.like.setOnClickListener(likeOnClick);
        holder.icLike.setOnClickListener(likeOnClick);
        holder.date.setText(StringUtils.dateFormat(commentEntity.getCreateAt(), "MM-dd HH:mm"));
        // 回复的点击监听
        View.OnClickListener replyOnclick = v -> {
            BaseActivity activity = (BaseActivity) mContext;
            String text = "回复[" + commentEntity.getUname() +"]：" + commentEntity.getContent();
            activity.showCommentFragment(commentEntity.getClassroomId(), commentEntity.getId(), text)
                    .setCommentListener(new CommentListener() {
                        @Override
                        public void onSuccess(CommentDTO c) {
                            Intent in = new Intent(mContext, CommentActivity.class);
                            in.putExtra("newComment", true);
                            in.putExtra("commentParent", commentEntity);
                            mContext.startActivity(in);
                        }

                        @Override
                        public void onFail() {

                        }
                    });
        };
        holder.icReply.setOnClickListener(replyOnclick);
        holder.reply.setOnClickListener(replyOnclick);
        holder.icMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] ops = new String[] {
                        "举报"
                };
                BottomHintFragment bottomHintFragment = BottomHintFragment.newInstance(ops, 203);
                bottomHintFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.dialogFullScreen);
                bottomHintFragment.show(((BaseActivity)mContext).getSupportFragmentManager(), BottomHintFragment.class.getSimpleName());
            }
        });
    }



    @Override
    public int getItemCount() {
        return commentEntityList.size();
    }

    static class CommentHolder extends RecyclerView.ViewHolder {
        View rootView;
        ImageView headPortrait;
        TextView name, content, comment, like, reply, date;
        IconfontView icLike, icComment, icReply, icMenu;

        public CommentHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            headPortrait = itemView.findViewById(R.id.head_portrait);
            name = itemView.findViewById(R.id.name);
            content = itemView.findViewById(R.id.tv_content);
            comment = itemView.findViewById(R.id.tv_comment);
            like = itemView.findViewById(R.id.tv_like);
            reply = itemView.findViewById(R.id.tv_reply);
            icLike = itemView.findViewById(R.id.ic_like);
            icComment = itemView.findViewById(R.id.ic_comment);
            icReply = itemView.findViewById(R.id.ic_reply);
            date = itemView.findViewById(R.id.tv_date);
            icMenu = itemView.findViewById(R.id.ic_comment_menu);
        }
    }

    private String generateLikeText(int likeNum) {
        return "点赞（" + likeNum + "）";
    }

    private String generateCommentText(int cmNum) {
        return "评论（" + cmNum + "）";
    }

    public void insertTo(int index, CommentDTO commentDTO) {
        this.commentEntityList.add(index, commentDTO);
    }

}
